import axios, { type AxiosInstance, type AxiosRequestConfig, type InternalAxiosRequestConfig } from "axios";
import type { interceptors } from "node_modules/axios/index.d.cts";

//axios请求配置
const config = {
    baseURl: 'http://localhost:9090',
    timeout: 10000
}

//定义返回值类型
export interface Result<T = any>{
    code: number,
    msg: string,
    data: T
}

class http{
    //axios实例
    private instance: AxiosInstance;

    //构造函数初始化
    constructor(config:AxiosRequestConfig){
        //创建axios实例
        this.instance = axios.create(config)
        //定义拦截器
        this.interceptors()
    }

    //拦截器
    private interceptors(){
        //axiso发送请求之前的处理
        this.instance.interceptors.request.use((config:InternalAxiosRequestConfig)=>{
            //请求头中携带token
            let token = ''
            if(token){
                config.headers!['token'] = token
            }
            return config
        },(error:any)=>{
            error.data = {}
            error.data.msg = '服务器异常'
            return error
        })
        //发请求之后的处理
    }
}